package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectPageDTO;
import com.zzyl.entity.Reservation;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReservationServiceImpl implements ReservationService {


    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询当日取消数量
     *
     * @return
     */
    @Override
    public Integer getCancelledCount() {
        Long userId = UserThreadLocal.getUserId();
        LocalDateTime dayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime dayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        //0：待报道，1：已完成，2：取消，3：过期
        Integer status = 2;
        Integer cancelledCount = reservationMapper.getCancelledCount(userId, dayStart, dayEnd, status);
        return cancelledCount;
    }

    /**
     * 查询每个时间段剩余的预约数量
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> countByTime(Long reservationDay) {
        HashMap<String, Object> map = new HashMap<>();
        //将毫秒装换成当前时间
        Instant instant = Instant.ofEpochMilli(reservationDay);
        LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();
        //定义当前时间
        LocalDateTime nowStart = LocalDateTime.of(localDate,LocalTime.now());
        //18：00结束预约
        LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.of(18, 0));
        //查询每个时间段已经有的预约人数，（固定时间段--在当前时间之后的剩下时间里面）
        List<Map<String,Object>> mapList = reservationMapper.countByTime(nowStart,endTime);
        //便利已查询出来的集合，处理集合
        //LocalDateTime start = LocalDateTime.of(localDate, LocalTime.of(8, 0));
        mapList.forEach(m->{
            Object time = m.get("time");
            m.put("time",time.toString());
        });

        return mapList;
    }

    /**
     * 新增预约
     *
     * @param reservation
     */
    @Override
    public void insert(Reservation reservation) {

        reservationMapper.insert(reservation);
    }

    /**
     * 分页条件查询
     *
     * @param reservationPageDTO@return
     */
    @Override
    public PageResponse<ReservationVo> pageCondition(NursingProjectPageDTO reservationPageDTO) {
        PageHelper.startPage(reservationPageDTO.getPageNum(),reservationPageDTO.getPageSize());
        reservationPageDTO.setUserId(UserThreadLocal.getUserId());
        Page<ReservationVo> page =  reservationMapper.pageCondition(reservationPageDTO);
        PageResponse<ReservationVo> response = PageResponse.of(page, ReservationVo.class);
        return response;
    }

    /**
     * 取消预约
     *
     * @param id
     */
    @Override
    public void cancel(Long id) {
        Reservation reservation = new Reservation();
        reservation.setId(id);
        reservation.setStatus(2);
        reservationMapper.update(reservation);
    }
}
